package com.spiderette.ace.sdk;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.applovin.sdk.AppLovinMediationProvider;
import com.applovin.sdk.AppLovinPrivacySettings;
import com.applovin.sdk.AppLovinSdk;
import com.applovin.sdk.AppLovinSdkConfiguration;
import com.applovin.sdk.AppLovinSdkInitializationConfiguration;
import com.spiderette.ace.sdk.ad.SAceMaxReward;
import com.spiderette.ace.sdk.utils.SAceAesUtils;
import com.spiderette.ace.sdk.utils.SAceSPUtils;

public class SDKManager {

    public static SDKManager getInstance() {
        return InstanceHolder.GAME_APPLICATION;
    }

    private static class InstanceHolder {
        private static final SDKManager GAME_APPLICATION = new SDKManager();
    }


    public void init(Activity activity) {
        Log.e("SAce", "onCreateonCreate");
        Context mApplication = activity.getApplicationContext();
        ((Application) mApplication).registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {

            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {
                SAceBusiness.onActivityCreate(activity);
            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {

            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {
            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {
            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {
                SAceBusiness.onActivityDestroyed(activity);
            }
        });

        SAceBusiness.init(mApplication);

        //AppLovinPrivacySettings
        AppLovinPrivacySettings.setHasUserConsent(true, mApplication);
        AppLovinPrivacySettings.setDoNotSell(false, mApplication);

        AppLovinSdkInitializationConfiguration initConfig = AppLovinSdkInitializationConfiguration.builder(
                        SAceAesUtils.decrypt(Constant.MAX_KEY, Constant.KEY_FLAG), mApplication)
                .setMediationProvider(AppLovinMediationProvider.MAX)
                .build();
        // Initialize the SDK with the configuration
        AppLovinSdk.getInstance(mApplication).initialize(initConfig, new AppLovinSdk.SdkInitializationListener() {
            @Override
            public void onSdkInitialized(final AppLovinSdkConfiguration config) {
//                AppLovinSdk.getInstance(context).showMediationDebugger();
                Log.e("SAce", "onSdkInitialized");
                SAceMaxReward.getInstance().loadRewardAd(mApplication);

                String country = SAceSPUtils.getString(Constant.KEY_COUNTRY,"");
                if (config != null && !TextUtils.isEmpty(config.getCountryCode())
                        && TextUtils.isEmpty(country)) {
                    Log.e("SAce", config.getCountryCode());
                    SAceSPUtils.putString(Constant.KEY_COUNTRY,config.getCountryCode());
                }
            }
        });

    }
}
